package org.zebra.captcha;

import cn.hutool.aop.aspects.Aspect;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.zebra.captcha.mail.MailCaptchaHandler;
import org.zebra.captcha.mail.MailCaptchaProperties;
import org.zebra.captcha.mail.MailCaptchaTemplateSource;
import org.zebra.captcha.picture.PictureCaptchaHandler;
import org.zebra.captcha.picture.PictureCaptchaProperties;
import org.zebra.captcha.sms.SmsCaptchaHandler;
import org.zebra.captcha.sms.SmsCaptchaProperties;
import org.zebra.captcha.sms.SmsCaptchaTemplateSource;
import org.zebra.message.MessageConfig;

import java.awt.*;

/**
 * @author zhanghongbin
 */
@AutoConfiguration(after = MessageConfig.class)
@EnableConfigurationProperties({PictureCaptchaProperties.class, MailCaptchaProperties.class, SmsCaptchaProperties.class
})
public class CaptchaConfig {

    private static final int WIDTH = 160;
    private static final int HEIGHT = 60;
    private static final Color BACKGROUND = Color.LIGHT_GRAY;
    private static final Font FONT = new Font("Arial", Font.BOLD, 48);

    @Value("${zebra.captcha.expiration-time:2}")
    // 过期时间 单位为分钟
    private int expirationTime = 2;

    @Value("${zebra.captcha.sms.code-length:4}")
    private int smsCodeLength = 4;

    @Value("${zebra.captcha.mail.code-length:4}")
    private int mailCodeLength = 4;

    /**
     * 圆圈干扰验证码
     *
     * @return 对象
     */
    @Lazy
    @Bean
    public CircleCaptcha circleCaptcha() {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(WIDTH, HEIGHT);
        captcha.setBackground(BACKGROUND);
        captcha.setFont(FONT);
        return captcha;
    }

    /**
     * 线段干扰的验证码
     *
     * @return 对象
     */
    @Lazy
    @Bean
    public LineCaptcha lineCaptcha() {
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(WIDTH, HEIGHT);
        captcha.setBackground(BACKGROUND);
        captcha.setFont(FONT);
        return captcha;
    }

    /**
     * 扭曲干扰验证码
     *
     * @return 对象
     */
    @Lazy
    @Bean
    public ShearCaptcha shearCaptcha() {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(WIDTH, HEIGHT);
        captcha.setBackground(BACKGROUND);
        captcha.setFont(FONT);
        return captcha;
    }

    @Bean
    public PictureCaptchaHandler pictureCaptchaHandler() {
        return new PictureCaptchaHandler(expirationTime);
    }

    @Bean
    public MailCaptchaHandler mailCaptchaHandler(
            @Autowired(required = false) MailCaptchaTemplateSource mailCaptchaTemplateSource,
            @Autowired(required = false) Aspect mailLogAspect) {
        if (mailCaptchaTemplateSource == null) {
            mailCaptchaTemplateSource = new MailCaptchaTemplateSource() {};
        }
        return new MailCaptchaHandler(mailCaptchaTemplateSource, mailLogAspect, expirationTime, mailCodeLength);
    }

    @Bean
    public SmsCaptchaHandler smsCaptchaHandler(
            @Autowired(required = false) SmsCaptchaTemplateSource smsCaptchaTemplateSource) {
        if (smsCaptchaTemplateSource == null) {
            smsCaptchaTemplateSource = new SmsCaptchaTemplateSource() {};
        }
        return new SmsCaptchaHandler(expirationTime, smsCodeLength, smsCaptchaTemplateSource);
    }

    @Bean
    // @DependsOn("pictureCaptchaHandler")
    public CaptchaController captchaController() {
        return new CaptchaController();
    }
}
